package controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
@RestController
@RequestMapping("/home")
public class HomeController {
    @GetMapping("index.do")
    public ModelAndView diIndexUI(ModelMap map, HttpSession session){
        System.out.printf("index.do");
        String username = (String)session.getAttribute("loginer");
        String date = LocalDate.now().toString();
        map.put("date",date);
        map.put("username",username);
        return new ModelAndView("index");
    }



    //访问路径:http://localhost:8080/spring_Mvc/handleLogin.do
    @PostMapping("handleLogin.do")

    public ModelAndView handleLogin3(String username, String password, ModelMap map,HttpSession session) {

        if ("江峰".equals(username)) {
            if ("8888".equals(password)) {
                map.put("message","登陆成功");
                session.setAttribute("loginer",username);
                System.out.printf("开始进行重定向");
                return new ModelAndView("redirect:index.do");
            }else{
                map.put("message","密码错误");
                return  new ModelAndView("message");
            }

        }
        else {
            map.put("message","用户名错误");
            return  new ModelAndView("message");


        }

    }
    @GetMapping("login.do")
    @ResponseBody
    public ModelAndView showLogin(){
        System.out.println("显示登录页面");
        return new ModelAndView("/login");
    }
}
